﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class LocatingRuleHeaderDO : BaseDO
    {


        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static LocatingRuleHeaderDO GetModelByPK(Session sess, string stLocatingName)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel + " WHERE LOCATING_RULE = @LOCATING_RULE ";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@LOCATING_RULE", stLocatingName)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据
        /// </summary>
        /// <remarks></remarks>
        public int Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "CFG_ILocatingRuleHeader";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        public void Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "CFG_DLocatingRule";

            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@LOCATING_RULE", this.LOCATING_RULE)});
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "CFG_ULocatingRuleHeader";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象LOCATINGRULEHEADER的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<LocatingRuleHeaderDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "LCR_RLocatingRule";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性LOCATINGRULEHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        /// 
        public static DataTable GetTableByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                stSQL = SelectSequel;
                helper = new DataHelper(sess);
                if (!StringManager.IsEmpty(columnName))
                {
                    stSQL = stSQL + " Where [" + columnName + "] =@Value ";
                    paras = new IDataParameter[]{
                                new SqlParameter( "@Value", value)
                            };
                }
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static List<LocatingRuleHeaderDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumn(sess, columnName, value);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        /// <summary>
        /// 从DataView装载持久性LOCATINGRULEHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<LocatingRuleHeaderDO> LoadListFromDataView(DataView dv)
        {
            List<LocatingRuleHeaderDO> list = new List<LocatingRuleHeaderDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static LocatingRuleHeaderDO GetModel(DataRow row)
        {
            LocatingRuleHeaderDO locRuleHdrDO = new LocatingRuleHeaderDO();
            if (!row["LOCATING_RULE"].Equals(DBNull.Value)) locRuleHdrDO.LOCATING_RULE = Convert.ToString(row["LOCATING_RULE"]);
            if (!row["DESCRIPTION"].Equals(DBNull.Value)) locRuleHdrDO.DESCRIPTION = Convert.ToString(row["DESCRIPTION"]);
            if (!row["ACTIVE"].Equals(DBNull.Value)) locRuleHdrDO.ACTIVE = Convert.ToString(row["ACTIVE"]);
            if (!row["OBJECT_ID"].Equals(DBNull.Value)) locRuleHdrDO.OBJECT_ID = Convert.ToInt32(row["OBJECT_ID"]);

            return locRuleHdrDO;
        }
        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [LOCATING_RULE_HEADER] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDataParameter[] ValueParas(LocatingRuleHeaderDO locRuleHdrDO)
        {
            SqlParameter[] paras = new SqlParameter[4];
            paras[0] = new SqlParameter("@LOCATING_RULE", locRuleHdrDO.LOCATING_RULE);
            paras[1] = new SqlParameter("@DESCRIPTION", locRuleHdrDO.DESCRIPTION);
            paras[2] = new SqlParameter("@ACTIVE", locRuleHdrDO.ACTIVE == String.Empty ? DBNull.Value : (object)locRuleHdrDO.ACTIVE);
            paras[3] = new SqlParameter("@OBJECT_ID", locRuleHdrDO.OBJECT_ID);

            paras[0].DbType = DbType.String;
            paras[1].DbType = DbType.String;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.Int32;

            return paras;
        }

        private static IDataParameter[] UpdateWherePara(LocatingRuleHeaderDO locRuleHdrDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@LOCATING_RULE", locRuleHdrDO.LOCATING_RULE);
            return paras;
        }

        #endregion
    }
}
